/*
 * Copyright 2009 Esko Suomi (suomi.esko@gmail.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package reflection.bpc.extraction;

import reflection.bpc.IBeanProperty;
import reflection.bpc.NonexistentPropertyException;
import reflection.bpc.BeanPropertyController.ExtractionDepth;

public class PropertyExtractor {
    
    private final ExtractionDepth extractionDepth;
    
    public PropertyExtractor(ExtractionDepth extractionDepth) {
        this.extractionDepth = extractionDepth;
    }
    
    public IBeanProperty<?> extractProperty(String propertyName, Object object) {
        IBeanProperty<Object> beanProperty = null;
       
        if (extractionDepth.compareTo(ExtractionDepth.METHODS) == 0) {
            beanProperty = new MethodExtractor().extractProperty(propertyName, object);
        }
        
        if (beanProperty == null && extractionDepth.compareTo(ExtractionDepth.FIELDS) >= 0) {
            beanProperty = new FieldExtractor().extractProperty(propertyName, object);
        }

        if (beanProperty == null && extractionDepth.compareTo(ExtractionDepth.QUESTIMATE) >= 0) {
            beanProperty = new DeclaredFieldExtractor().extractProperty(propertyName, object);                
        }
        
        if (beanProperty != null) {            
            return beanProperty;
        }
        
        throw new NonexistentPropertyException(propertyName, object.getClass());
    }
}
